#!/usr/bin/env sh

OS_NAME="FreeBSD"
print() {
  echo "<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>"
  echo "<> ${OS_NAME}: $@" | fold
  #echo "<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>"
  echo ""
}

IN_INSTALL() {
    print "update pkg mirror"
    mkdir -p /usr/local/etc/pkg/repos/
    echo 'FreeBSD: {' > /usr/local/etc/pkg/repos/FreeBSD.conf
    read -p "Are you using FreeBSD-CURRENT? [y/N]: " changed
    if echo "$changed" | grep -iq "^y"; then
        echo '  url: "pkg+http://mirrors.ustc.edu.cn/freebsd-pkg/${ABI}/latest",' >> /usr/local/etc/pkg/repos/FreeBSD.conf
    else
        echo '  url: "pkg+http://mirrors.ustc.edu.cn/freebsd-pkg/${ABI}/quarterly",' >> /usr/local/etc/pkg/repos/FreeBSD.conf
    fi
    echo '}' >> /usr/local/etc/pkg/repos/FreeBSD.conf
	pkg update -f
	pkg upgrade

    print "update ports mirror"
    echo "MASTER_SITE_OVERRIDE?=http://mirrors.ustc.edu.cn/freebsd-ports/distfiles/\${DIST_SUBDIR}/" >> /etc/make.conf
    freebsd-update fetch
    freebsd-update install

    IN_INSTALL_DESKTOP_P
}

IN_INSTALL_DESKTOP_P() {
  read -p "Install Desktop? [y/N]: " hasx
  if echo "$hasx" | grep -iq "^y"; then
    print "Install Desktop Environments:"
    echo "  1) i3               2) EXWM"
    echo "  3) Cinnamon         4) Mate"
    echo "  5) GNOME3           6) KDE(Plasma)"
    echo "  7) Xfce             8) Enlightments"
    echo "  9) JWM             10) NsCDE"
    echo " 11) LXDE            12) LXQt"
    read -p "Select your Desktop: " card
    case $card in
      1) IN_INSTALL_DE "i3" "elogind-${ARTIX_INIT} lxdm-${ARTIX_INIT}" elogind lxdm "i3-gaps i3blocks i3lock i3status feh dmenu rofi picom lxterminal pcmanfm leafpad" ;;
      2) IN_INSTALL_DE "emacs" "elogind-${ARTIX_INIT} lxdm-${ARTIX_INIT}" elogind lxdm "emacs dmenu rofi lxterminal pcmanfm leafpad" ;;
      3) IN_INSTALL_DE "cinnamon-session" "elogind-${ARTIX_INIT} gdm-${ARTIX_INIT}" elogind gdm "cinnamon gnome-terminal" ;;
      4) IN_INSTALL_DE "mate-session" "elogind-${ARTIX_INIT} gdm-${ARTIX_INIT}" elogind gdm "mate-base mate-common mate-desktop mate-terminal" ;;
      5) IN_INSTALL_DE "gnome-session" "elogind-${ARTIX_INIT} gdm-${ARTIX_INIT}" elogind gdm "gnome gnome-extra" ;;
      6) IN_INSTALL_DE "startplasma-x11" "elogind-${ARTIX_INIT} sddm-${ARTIX_INIT}" elogind sddm "plasma5-plasma" ;;
      7) IN_INSTALL_DE "xfce4-session" "elogind-${ARTIX_INIT} xdm-${ARTIX_INIT}" elogind xdm "xfce xfce4-goodies" ;;
      8) IN_INSTALL_DE "enlightenment_start" "elogind-${ARTIX_INIT} lightdm-${ARTIX_INIT}" elogind lightdm "enlightenment terminology" ;;
      9) IN_INSTALL_DE "jwm" "elogind-${ARTIX_INIT} lxdm-${ARTIX_INIT}" elogind lxdm "jwm dmenu rofi lxterminal pcmanfm leafpad" ;;
      10) IN_INSTALL_DE "nscde" "elogind-${ARTIX_INIT} lxdm-${ARTIX_INIT}" elogind lxdm "nscde" ;;
      11) IN_INSTALL_DE "startlxde" "elogind-${ARTIX_INIT} lxdm-${ARTIX_INIT}" elogind lxdm "lxde-meta" ;;
      12) IN_INSTALL_DE "startlxqt" "elogind-${ARTIX_INIT} sddm-${ARTIX_INIT}" elogind sddm "lxqt" ;; # breeze-icons oxygen-icons buuf-deuce-kde-icon-theme
      *) echo "Skip install desktop environments." ;;
    esac
    IN_INSTALL_VIDEO_CARD_DRIVER
  fi
}

IN_INSTALL_DE() {
  print "Install Desktop:"
  local exec_name="$1"
  local de_service="$2"
  local logind="$3"
  local loginm="$4"

  local de_soft="$5"
  local de_list="$de_soft xorg firefox vlc zile wqy-fonts jetbrains-mono fcitx5 json-c fcitx5-configtool zh-fcitx5-chinese-addons"

  [ "$exec_name" == "i3" ] && [ ! -n "$(command -v i3)" ] && i3_need_cfg=1
  [ "$exec_name" == "emacs" ] && curl -fksSL https://gitee.com/damon-kwok/oh-my-env/raw/main/INSTALL | sh

  read -p "Install Login Manager? [y/N]: " haslm
  if echo "$haslm" | grep -iq "^y"; then
    pkg install $de_list $de_service
    #rc-update add dbus default
    #rc-update add $logind default
    #rc-update add $loginm default
  else
    pkg install $de_list
    echo "exec $exec_name" > ~/.xinitrc
  fi
  [ "$i3_need_cfg" == "1" ] && IN_INSTALL_I3_CONFIG

  print "$exec_name install complate."
}

IN_INSTALL_I3_CONFIG() {
  # extra
  print "update i3 config:"
  mkdir -p /usr/share/backgrounds/
  curl -o /usr/share/backgrounds/archlinux1.jpg https://cdn.wallpapersafari.com/67/23/m83TIS.jpg
  curl -o /usr/share/backgrounds/archlinux2.png http://wallpoper.com/images/00/39/10/60/linux-arch_00391060.png
  curl -o /usr/share/backgrounds/archlinux3.png http://p7.itc.cn/q_70/images03/20200617/c10cd5eb4c604784bf814e5d35cfe0ca.png
  curl -o /usr/share/backgrounds/archlinux4.jpg https://wallpapercave.com/wp/p51XiSX.jpg
  echo "#!/bin/sh" > ~/.fehbg
  echo "feh --no-fehbg --bg-scale '/usr/share/backgrounds/archlinux.png'" > ~/.fehbg
  if [ -e "~/.config/i3/config" ]; then
    echo "exec_always --no-startup-id picom" >> ~/.config/i3/config
    echo "exec_always --no-startup-id fcitx5" >> ~/.config/i3/config
    echo "exec_always --no-startup-id feh --randomize --bg-fill /usr/share/backgrounds/*" >> ~/.config/i3/config

    sed -i 's/bindsym \$mod+d/# bindsym \$mod+d/g' ~/.config/i3/config
    echo "bindsym \$mod+d exec rofi -show run" >> ~/.config/i3/config
    echo "bindsym \$mod+z exec firefox" >> ~/.config/i3/config
    echo "bindsym \$mod+x exec pcmanfm" >> ~/.config/i3/config
  fi
}

IN_INSTALL_DESKTOP_UTILS() {
  echo "#!/usr/bin/env sh" > /usr/bin/vpn
  echo "sudo openconnect https://fmt1.link.ac.cn --authgroup smart" >> /usr/bin/vpn
  chmod +x /usr/bin vpn

  #mkdir -p /usr/share/backgrounds/
  #curl -o /usr/share/backgrounds/archlinux.png http://wallpoper.com/images/00/39/10/60/linux-arch_00391060.png
  #feh --bg-scale /usr/share/backgrounds/archlinux.png

  trizen -Syyu
  trizen -S firefox bcompare wps-office-cn wechat-uos xunlei-bin termius-app
}

IN_INSTALL_VIDEO_CARD_DRIVER() {
  print "Install video card driver:"
  echo "  1) amdgpu               2) ati"
  echo "  3) dummy                4) intel"
  echo "  5) nvidia               6) nouveau"
  echo "  7) openchrome           8) sisusb"
  echo "  9) virtualbox          10) vmware"
  echo " 11) voodoo              12) vesa"
  read -p "Select your video card: " card
  case $card in
    1) pkg install xf86-video-amdgpu ;;
    2) pkg install xf86-video-ati ;;

    3) pkg install xf86-video-dummy ;;
    4) pkg install xf86-video-intel ;;

    5) pkg install nvidia-driver ;;
    6) pkg install xf86-video-nouveau ;;

    7) pkg install xf86-video-openchrome ;;
    8) pkg install xf86-video-sisusb ;;

    9) pkg install virtualbox-ose-additions ;;
    10) pkg install xf86-video-vmware ;;

    11) pkg install xf86-video-voodoo ;;
    12) pkg install xf86-video-vesa ;;
    *) echo "Skip install video card driver." ;;
  esac
}

IN_INSTALL_STEAM() {
    kldload linux
    kldload linux64
    pkg install linux-steam-utils
    #
    echo "tmpfs    /compat/linux/dev/shm  tmpfs   rw,mode=1777    0       0" >> /etc/fstab
    echo "linprocfs   /compat/linux/proc  linprocfs       rw      0       0" >> /etc/fstab
    echo "linsysfs   /compat/linux/sys  linsysfs       rw      0       0" >> /etc/fstab
    echo "fdesc /dev/fd fdescfs rw 0 0" >> /etc/fstab
    echo "proc  /proc   procfs  rw 0 0" >> /etc/fstab
    #
    echo 'linux_load="YES"' >> /boot/loader.conf
    echo 'linux64_load="YES"' >> /boot/loader.conf
}

case $1 in
  -i|--install) IN_INSTALL;;
  -g|--game|--steam) IN_INSTALL_STEAM;;
  -g2) steam-install --allow-stealing-my-passwords,-browser-history-and-ssh-keys;;
  -x) IN_INSTALL_DESKTOP_P;;
  -i3) IN_INSTALL_I3_CONFIG;;
  -ome) curl -fksSL https://gitee.com/damon-kwok/oh-my-env/raw/main/INSTALL | sh;;
  *)
    echo "$OS_NAME minimal installation script"
    echo "  -i, --install      Install $OS_NAME"
    echo "  -g, --steam        Install steam..."
esac

